<?php

namespace app\api\library\promotion\addPromotionCustomer;

use think\facade\Db;

/**
 *
 * User: chenzheng
 * Date: 2023/5/20
 * Time: 11:25
 */
class V
{
    public static function run($param)
    {
        $promotionId = _checkEmptyParam($param[ 'promotionId' ], '推广人不能为空');
        $userId      = _checkEmptyParam($param[ 'userId' ], '用户不能为空');
        if ($promotionId==$userId){
            _outPut(204, [], '推广人不能添加自己');
        }
        $promotion = Db::name('mall_promotion')->where('user_id', $promotionId)->find();
        if ( !empty($promotion)) {
            if ($promotion[ 'status' ] == 0) {
                _outPut(204, [], '推广人正在申请推广中');
            }
        } else {
            _outPut(204, [], '该推广人未被申请');
        }
        $user = Db::name('mall_user')->where('id', $userId)->find();
        if ( !empty($user[ 'promotion_id' ])) {
            _outPut(204, [], '该用户已绑定推广人');
        }
        $updateData = [
            'promotion_id'   => $promotionId,
            'promotion_time' => time(),
        ];
        if (false === Db::name('mall_user')->where('id', $userId)->update($updateData)) {
            _outPut(204, [], '绑定推广人失败');
        }
        
        _outPut(200, [], '绑定成功');
    }
}